//
//  ClothesViewController.m
//  DWL_Vogue
//
//  Created by dllo on 15/10/22.
//  Copyright (c) 2015年 Forlosan. All rights reserved.
//

#import "ClothesViewController.h"
#import "VogueHeader.h"
#import "WidthAndHeight.h"
#import "ClothesCollectionViewCell.h"


@interface ClothesViewController ()< UITableViewDelegate, UITableViewDataSource>
@property(nonatomic, retain)UITableView *tableView;
@property(nonatomic, retain)NSMutableArray *clothesVogueArr;
@property(nonatomic, retain)UIView *headView;
@property(nonatomic, retain)UIButton *leftButton;
@property(nonatomic, retain)UIButton *rightbutton;
@property(nonatomic, retain)UILabel *centerLabel;
@property(nonatomic, retain)UICollectionView *collectionView;
@property(nonatomic, assign)NSInteger num;

@end

@implementation ClothesViewController
- (void)dealloc {
    [_tableView release];
    [_headView release];
    [_clothesVogueArr release];
    [_centerLabel release];
    [_collectionView release];
    
    [super dealloc];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.navigationController.navigationBar.barTintColor = [UIColor colorWithRed:39 / 255.0 green:38 / 255.0 blue:55 / 255.0 alpha:1];
    UILabel *clothesTitle = [[UILabel alloc] initWithFrame:CGRectMake(WIDTH / 2 - 30, 15, 60, 20)];
    clothesTitle.text = @"潮流·穿";
    clothesTitle.textAlignment = NSTextAlignmentCenter;
    clothesTitle.textColor = [UIColor whiteColor];
    clothesTitle.font = [UIFont fontWithName:@"ArialRoundedMTBold" size:15];
    [self.navigationController.navigationBar addSubview:clothesTitle];
    [clothesTitle release];
    
    //    TableView
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT) style:UITableViewStylePlain];
    self.tableView.backgroundColor = [UIColor colorWithRed:0.275 green:0.305 blue:0.337 alpha:1.000];
    [self.tableView setSeparatorColor:[UIColor colorWithRed:0.213 green:0.237 blue:0.259 alpha:1.000]];
    [self.view addSubview:self.tableView];
    self.tableView.backgroundColor = [UIColor colorWithRed:0.275 green:0.305 blue:0.337 alpha:1.000];
    [_tableView release];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.bounces = YES;
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.tableView.footer = [MJRefreshAutoNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(footer)];
    
////    Flowlayout
//    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
//    flowLayout.itemSize = CGSizeMake(WIDTH / 2 - 50, HEIGHT / 3);
//    flowLayout.minimumLineSpacing = 10;
//    flowLayout.minimumInteritemSpacing = 10;
//    flowLayout.sectionInset = UIEdgeInsetsMake(50, 20, 20, 20);
    
////    CollectionView
//    self.collectionView = [[UICollectionView alloc] initWithFrame:self.view.frame collectionViewLayout:flowLayout];
//    [self.view addSubview:self.collectionView];
//    [flowLayout release];
//    [_collectionView release];
//    self.collectionView.backgroundColor = [UIColor whiteColor];
//    self.collectionView.dataSource = self;
//    self.collectionView.delegate = self;
//    [self.collectionView registerClass:[ClothesCollectionViewCell class] forCellWithReuseIdentifier:@"reuse"];
    
////    back
//    UIImageView *backImage = [[UIImageView alloc]initWithFrame:self.view.frame];
//    backImage.image = [UIImage imageNamed:@"backColoe.jpg"];
    

////    HeadView
//    self.headView = [[UIView alloc] initWithFrame:CGRectMake(0, 44, WIDTH, 30)];
//    [self.navigationController.navigationBar addSubview:self.headView];
//    self.headView.backgroundColor = [UIColor colorWithWhite:0.200 alpha:1.000];
//    [_headView release];
//    self.leftButton = [UIButton buttonWithType:UIButtonTypeSystem];
//    self.leftButton.frame = CGRectMake(0, 0, (WIDTH - 3) / 2, 30);
//    [self.leftButton setTitle:@"♂" forState:UIControlStateNormal];
//    self.leftButton.titleLabel.font = [UIFont fontWithName:@"Helvetica-Bold" size:22];
//    self.leftButton.tintColor = [UIColor whiteColor];
//    [self.headView addSubview:self.leftButton];
//    [_leftButton release];
//    [self.leftButton addTarget:self action:@selector(leftButtonAction) forControlEvents:UIControlEventTouchUpInside];
//    self.rightbutton = [UIButton buttonWithType:UIButtonTypeSystem];
//    self.rightbutton.frame = CGRectMake((WIDTH - 1) / 2 , 0, (WIDTH - 1) / 2, 30);
//    [self.rightbutton setTitle:@"♀" forState:UIControlStateNormal];
//    self.rightbutton.titleLabel.font = [UIFont fontWithName:@"Helvetica-Bold" size:25];
//    self.rightbutton.tintColor = [UIColor whiteColor];
//    [self.headView addSubview:self.rightbutton];
//    [_rightbutton release];
//    [self.rightbutton addTarget:self action:@selector(rightButtonAction) forControlEvents:UIControlEventTouchUpInside];
//    self.centerLabel = [[UILabel alloc] initWithFrame:CGRectMake((WIDTH - 1) / 2 - 1, 0, 1, 30)];
////    self.centerLabel.text = @"|";
////    self.centerLabel.textColor = [UIColor whiteColor];
//    self.centerLabel.layer.borderWidth = 1;
//    self.centerLabel.layer.borderColor = [UIColor lightGrayColor].CGColor;
//    [self.headView addSubview:self.centerLabel];
//    [_centerLabel release];
    
//    CreateData
    self.num = 20;
    [self createData];

}

- (void)leftButtonAction {
    self.leftButton.backgroundColor = [UIColor lightGrayColor];
    self.rightbutton.backgroundColor = [UIColor clearColor];
}

- (void)rightButtonAction {
    self.rightbutton.backgroundColor = [UIColor lightGrayColor];
    self.leftButton.backgroundColor =[UIColor clearColor];
}

- (void)createData {
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    [manager.requestSerializer setValue:@"JSESSIONID=LEqY9DRPCLwJflEPh17-cA" forHTTPHeaderField:@"Cookie"];
    [manager GET:@"http://a1.go2yd.com/Website/channel/news-list-for-channel?appid=dress&cend=30&channel_id=1462397707&cstart=0&cv=1.1.3&distribution=com.apple.appstore&fields%5B%5D=title&fields%5B%5D=url&fields%5B%5D=source&fields%5B%5D=date&fields%5B%5D=image&fields%5B%5D=image_urls&fields%5B%5D=comment_count&fields%5B%5D=like&infinite=true&net=wifi&platform=0&refresh=1&version=010904" parameters:nil success:^(NSURLSessionDataTask *task, id responseObject) {
        self.clothesVogueArr = [NSMutableArray array];
        for (NSDictionary *dic in responseObject[@"result"]) {
            NSMutableArray *arr = dic[@"image_urls"];
            if (arr.count == 3) {
                MoreCellInformation *moreCell = [[MoreCellInformation alloc] init];
                [moreCell setValuesForKeysWithDictionary:dic];
                [self.clothesVogueArr addObject:moreCell];
                [moreCell release];
            }
        }
        [self.tableView reloadData];
    } failure:^(NSURLSessionDataTask *task, NSError *error) {
        NSLog(@"%@", error);
    }];

}

- (void)footer {
    NSString *str1 = [NSString string];
    NSString *str2 = [NSString string];
    if (self.num <= 80) {
        self.num = self.num + 30;
        str1 = [NSString stringWithFormat:@"http://a1.go2yd.com/Website/channel/news-list-for-channel?appid=dress&cend=%ld", self.num];
        str2 = [NSString stringWithFormat:@"&channel_id=1462397707&cstart=%ld", self.num - 30];
    } else {
        self.num = self.num + 29;
        str1 = [NSString stringWithFormat:@"http://a1.go2yd.com/Website/channel/news-list-for-channel?appid=dress&cend=%ld", self.num];
        str2 = [NSString stringWithFormat:@"&channel_id=1462397707&cstart=%ld", self.num - 30];
    }
    NSString *str3 = @"&cv=1.1.3&distribution=com.apple.appstore&fields%5B%5D=title&fields%5B%5D=url&fields%5B%5D=source&fields%5B%5D=date&fields%5B%5D=image&fields%5B%5D=image_urls&fields%5B%5D=comment_count&fields%5B%5D=like&infinite=true&net=wifi&platform=0&refresh=1&version=010904";
    NSString *str4 = [[str1 stringByAppendingString:str2] stringByAppendingString:str3];
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    [manager.requestSerializer setValue:@"JSESSIONID=LEqY9DRPCLwJflEPh17-cA" forHTTPHeaderField:@"Cookie"];
    [manager GET:str4 parameters:nil success:^(NSURLSessionDataTask *task, id responseObject) {
        for (NSDictionary *dic in responseObject[@"result"]) {
            NSMutableArray *arr = dic[@"image_urls"];
            if (arr.count == 3) {
                MoreCellInformation *moreCell = [[MoreCellInformation alloc] init];
                [moreCell setValuesForKeysWithDictionary:dic];
                [self.clothesVogueArr addObject:moreCell];
                [moreCell release];
            }
        }
        NSLog(@"%ld", self.num);
        [self.tableView.footer endRefreshing];
        [self.tableView reloadData];
    } failure:^(NSURLSessionDataTask *task, NSError *error) {
        NSLog(@"%@", error);
    }];
    
}


#pragma mark  CollectionView
//- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
//    return 10;
//}
//
//- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
//    ClothesCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"reuse" forIndexPath:indexPath];
////    cell.clothesImageView
//    cell.clothesLabel.text = @"12";
//    return cell;
//}


#pragma mark  TableView
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.clothesVogueArr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *reuse1 = @"reuse1";
    MoreFoodTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:reuse1];
    if (!cell) {
        cell = [[[MoreFoodTableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:reuse1] autorelease];
    }
    cell.backgroundColor = [UIColor colorWithRed:0.275 green:0.305 blue:0.337 alpha:1.000];
    cell.moreTitleLabel.text = [self.clothesVogueArr[indexPath.row] titleName];
    cell.moreTextLabel.text = [@"# 潮流 · 街拍 & " stringByAppendingString:[self.clothesVogueArr[indexPath.row] source]];
    NSString *str1 = [self.clothesVogueArr[indexPath.row] image_urls][0];
    NSString *str2 = [self.clothesVogueArr[indexPath.row] image_urls][1];
    NSString *str3 = [self.clothesVogueArr[indexPath.row] image_urls][2];
    NSString *url1 = [[@"http://i3.go2yd.com/image.php?url=" stringByAppendingString:str1] stringByAppendingString:@"&type=thumbnail_900x000"];
    NSLog(@"%@", url1);
    NSString *url2 = [[@"http://i3.go2yd.com/image.php?url=" stringByAppendingString:str2] stringByAppendingString:@"&type=thumbnail_900x000"];
    NSString *url3 = [[@"http://i3.go2yd.com/image.php?url=" stringByAppendingString:str3] stringByAppendingString:@"&type=thumbnail_900x000"];
    [cell.moreImageView1 sd_setImageWithURL:[NSURL URLWithString:url1] placeholderImage:[UIImage imageNamed:@"placeholderSmall.jpg"]];
    [cell.moreImageView2 sd_setImageWithURL:[NSURL URLWithString:url2] placeholderImage:[UIImage imageNamed:@"placeholderSmall.jpg"]];
    [cell.moreImageView3 sd_setImageWithURL:[NSURL URLWithString:url3] placeholderImage:[UIImage imageNamed:@"placeholderSmall.jpg"]];
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    [cell setSelectionStyle:UITableViewCellSelectionStyleNone];
    ClothesWebViewController *web = [[ClothesWebViewController alloc] init];
    web.url = [self.clothesVogueArr[indexPath.row] urlid];
    [self.navigationController pushViewController:web animated:YES];
    [web release];
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
        return WIDTH / 4 + 75;
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
